package cn.edu.hnu.matching.toolcalling;

import cn.dev33.satoken.stp.StpUtil;
import cn.edu.hnu.matching.constants.UserConstants;
import cn.edu.hnu.matching.mapper.UserMapper;
import cn.edu.hnu.matching.model.entity.User;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;

/**
 * Tool Calling 方法实现
 */
@Component
@AllArgsConstructor
public class AiChatTool {

    private final UserMapper userMapper;

    @Tool(description = "根据当前用户登录token，获取当前用户对象相关信息")
    public User getCurrentUserId(String token) {
        Object loginId = StpUtil.getLoginIdByToken(token);
        // 当前用户未登录
        if (ObjectUtil.isEmpty(loginId)) {
            User user = new User();
            user.setId(-1L);
            return user;
        }
        // 转 openId
        String openId = (String) loginId;
        return userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getOpenId, openId)
                .eq(User::getIsDelete, UserConstants.UN_DELETED)
        );
    }

}
